package by.grsu.hdm.devices;

import java.util.Random;

import by.grsu.hdm.media.AudioFormat;
import by.grsu.hdm.media.IMediaPlayerSupport;
import by.grsu.hdm.media.MediaPlayer;
import by.grsu.hdm.media.VideoFormat;
import by.grsu.hdm.media.remotecontrol.Button;
import by.grsu.hdm.media.remotecontrol.RemoteControl;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport,
		INetworkAvailable, IMediaPlayerSupport {

	private long trafficIn;
	private long trafficOut;

	private UsbPort usbPort;
	private MediaPlayer mediaPlayer;

	protected MediaCenter() {
		defaultConfiguration();
	}

	public MediaCenter(String id) {
		super(id);

		defaultConfiguration();
	}

	protected void defaultConfiguration() {
		this.trafficIn = 0;
		this.trafficOut = 0;

		this.mediaPlayer = new MediaPlayer("audio_video_player",
				new RemoteControl());
		this.mediaPlayer.registerFormat(new AudioFormat("mp3"));
		this.mediaPlayer.registerFormat(new VideoFormat("avi"));
		this.mediaPlayer.getRemoteControl().addButtons(
				new Button("start", "start player "));
		this.mediaPlayer.getRemoteControl().addButtons(
				new Button("pause", "pause player "));
		this.mediaPlayer.getRemoteControl().addButtons(
				new Button("stop", "stop player "));
		this.usbPort = null;
	}

	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}

	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
	}

	@Override
	protected void beforeTurningOff() {
		defaultConfiguration();
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) {
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}

		return this.usbPort;
	}

	@Override
	public MediaPlayer getMediaPlayer() {
		return this.mediaPlayer;
	}
}
